/*
 *  Filename:      gsl_defs.h
 *  Author:         Erick Huang<erickhuang1989@gmail.com>
 *  Revised:       2013-06-03
 *  Description:   This file contains useful macros and data types
 *
 *  Copyright (c) Erick Huang. All rights reserved.
 */

#ifndef GSL_DEFS_H
#define GSL_DEFS_H

#ifdef __cplusplus
extern "C" {
#endif  /*__cplusplus*/


#include "gsl_types.h"



//Gets the length of the array
#define ARRAY_SIZE(arr)  (sizeof(arr) / sizeof((arr)[0]))

#define HALF_VAL(val)  ((val) >> 1)

#ifndef MAX
#define MAX(a,b) (((a)>(b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) (((a)<(b)) ? (a) : (b))
#endif
#ifndef ABS
#define ABS(n)   (((n) < 0) ? -(n) : (n))
#endif

#define SETBIT(x,mask)    	(   (x) | (mask)     )
#define CLRBIT(x,mask)    	(  	(x) & (~(mask)) )
#define TESTBIT(x,mask)	  	(   (x) & (mask)     )

/* takes a byte out of a UINT32 : var - uint32;  ByteNum - byte to take out (0 ~ 3) */
#define BREAK_UINT32( var, ByteNum ) \
          (UINT8)((UINT32)(((var) >>((ByteNum) * 8)) & 0x00FF))

#define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3)     \
          ((UINT32)(  (UINT32)((Byte0) & 0x00FF) \
          + ((UINT32)((Byte1) & 0x00FF) << 8)       \
          + ((UINT32)((Byte2) & 0x00FF) << 16)      \
          + ((UINT32)((Byte3) & 0x00FF) << 24))  )

#define BUILD_UINT16(loByte, hiByte) \
          ((UINT16)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))

#define HI_UINT16(a) (((a) >> 8) & 0xFF)
#define LO_UINT16(a) ((a) & 0xFF)

#define BUILD_UINT8(hiByte, loByte) \
          ((UINT8)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4)))

#define HI_UINT8(a) (((a) >> 4) & 0x0F)
#define LO_UINT8(a) ((a) & 0x0F)


/*
 *	This macro is for use by other macros to form a fully valid C statement.
 *	Without this, the if/else conditionals could show unexpected behavior.
 *   (The while condition below evaluates false without generating a
 *    constant-controlling-loop type of warning on most compilers.)
 */
#define st(x)      do { x } while (__LINE__ == -1)





#ifdef __cplusplus
}
#endif  /*__cplusplus*/

#endif  /* GSL_DEFS_H */

